﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Waiter.Common;

namespace Waiter.Controler
{
    class FunitureCtrl
    {
        public List<FUNITURE> fsGetFunitureList()
        {
            try
            {
                return (from s in CommonGlobalVariable.globalDataContext.FUNITUREs
                           where s.DEL_FLG == CommonConstants.FLAG_OFF
                           select s).ToList();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "FunitureCtrl.fsGetFunitureList",
                    "",
                    ex.ToString());
                return null;
            }
        }

        public List<FUNITURE_D> fsGetFunitureDList(string funitureCode)
        {
            try
            {
                return (from s in CommonGlobalVariable.globalDataContext.FUNITURE_Ds
                        where s.FUNITURE_CODE == funitureCode
                        orderby s.UPD_DATE
                        select s).ToList();
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "FunitureCtrl.fsGetFunitureList", 
                    CommonUtils.fsParamListToString(new object[1]{funitureCode}), 
                    ex.ToString());
                return null;
            }
        }

        public FUNITURE fsFindFunitureByCode(string funitureCode)
        {
            try
            {
                return CommonGlobalVariable.globalDataContext.FUNITUREs.FirstOrDefault(q => q.CODE == funitureCode);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "FunitureCtrl.fsFindFunitureByCode",
                    CommonUtils.fsParamListToString(new object[1] { funitureCode }),
                    ex.ToString());
                return null;
            }
        }

        public FUNITURE_D fsFindFunitureDByCode(string funitureDCode)
        {
            try
            {
                return CommonGlobalVariable.globalDataContext.FUNITURE_Ds.FirstOrDefault(q => q.CODE == funitureDCode);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "FunitureCtrl.fsFindFunitureDByCode",
                    CommonUtils.fsParamListToString(new object[1] { funitureDCode }),
                    ex.ToString());
                return null;
            }
        }

        public FUNITURE fsFindFunitureByName(string funitureName)
        {
            try
            {
                return CommonGlobalVariable.globalDataContext.FUNITUREs.FirstOrDefault(q => q.FUNITURE_NAME == funitureName && q.DEL_FLG == CommonConstants.FLAG_OFF);
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "FunitureCtrl.fsFindFunitureByName",
                    CommonUtils.fsParamListToString(new object[1] { funitureName }),
                    ex.ToString());
                return null;
            }
        }

        public ResultStatus fsInsertFuniture(FUNITURE item)
        {
            try
            {
                CommonGlobalVariable.globalDataContext.FUNITUREs.InsertOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "FunitureCtrl.fsInsertFuniture",
                    CommonUtils.fsParamListToString(new object[1] { item }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
        }

        public ResultStatus fsInsertFunitureD(FUNITURE_D item)
        {
            try
            {
                CommonGlobalVariable.globalDataContext.FUNITURE_Ds.InsertOnSubmit(item);
                CommonGlobalVariable.globalDataContext.SubmitChanges();
                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "FunitureCtrl.fsInsertFunitureD",
                    CommonUtils.fsParamListToString(new object[1] { item }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
        }

        public ResultStatus fsDeleteFuniture(string funitureCode)
        {
            try
            {
                FUNITURE item = fsFindFunitureByCode(funitureCode);
                item.DEL_FLG = CommonConstants.FLAG_ON;
                CommonGlobalVariable.globalDataContext.SubmitChanges();
                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "FunitureCtrl.fsDeleteFuniture",
                    CommonUtils.fsParamListToString(new object[1] { funitureCode }),
                    ex.ToString());
                return ResultStatus.ERROR;
            }
        }

        public ResultStatus fsSaveChange()
        {
            try
            {
                CommonGlobalVariable.globalDataContext.SubmitChanges();
                return ResultStatus.SUCCESS;
            }
            catch (Exception ex)
            {
                CommonLogging.fsWriteLog(CommonConstants.LOG_TYPE_ERROR,
                    "FunitureCtrl.fsSaveChange",
                    "",
                    ex.ToString());
                return ResultStatus.ERROR;
            }
        }
    }
}
